<template>
  <div class="data-table-container">
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="名称" min-width="120" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0">待审核</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status === 1">审核中</el-tag>
          <el-tag type="success" v-else-if="scope.row.status === 2">审核通过</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === 3">审核拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination-container" v-if="showPagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 定义props
const props = defineProps({
  tableData: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  total: {
    type: Number,
    default: 0
  },
  showPagination: {
    type: Boolean,
    default: true
  }
})

// 定义emits
const emit = defineEmits(['view', 'edit', 'delete', 'size-change', 'current-change'])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)

// 处理查看
const handleView = (row: any) => {
  emit('view', row)
}

// 处理编辑
const handleEdit = (row: any) => {
  emit('edit', row)
}

// 处理删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    emit('delete', row)
  })
}

// 处理分页大小变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  emit('size-change', val)
}

// 处理当前页变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  emit('current-change', val)
}
</script>

<style scoped lang="scss">
.data-table-container {
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>